import Api from './api'
import Address from './address'

const getUserTest = async (page = 1) => {
  let url = Address.getUserTest() + Address.getPageParams('?', page)
  let res = await Api.netFetch(url)
  if (res && res.result) {
    let data = res.data.data
    data.forEach((item) => {
      let ex = {
        id: item.id,
        name: item.name,
        image: item.image_path,
        desc: item.min_desc,
        count: item.view_count,
        is: item.isUserEvaluation,
      }
      item.ex = ex
    })
    return {
      data: data,
      result: res.result,
    }
  }
}

const getTestDesc = async (id) => {
  let url = Address.getUserTest() + `/${id}`
  let res = await Api.netFetch(url)
  if (res && res.result) {
    let data = res.data.data
    return {
      data: data,
      result: res.result,
    }
  }
}
/**
 * 获取答题进度
 * @param id
 */
const getAnswer = async (id) => {
  let url = Address.getAnswer(id)
  let res = await Api.netFetch(url)
  if (res && res.result) {
    let data = res.data.data
    return {
      data: data,
      result: res.result,
    }
  }
}
/**
 * 保存答案
 * @param param
 * @returns {Promise<{data: *, result: *}>}
 */
const saveAnswers = async (param) => {
  let url = Address.saveAnswers()
  let res = await Api.netFetch(url, 'POST', param, true)
  if (res && res.result) {
    let data = res.data.data
    return {
      data: data,
      result: res.result,
    }
  }
}
/**
 * 获取用户测评结果
 * @param type
 * @returns {Promise<{data: *, result: *}>}
 */
const getEvaluationResult = async (user,test) => {
  let url = Address.getEvaluationResult(user,test)
  // let res = await Api.netFetch(url, 'GET', null, false, {Accept: 'application/vnd.github.html'}, 'text');
  let res = await Api.netFetch(url)
  if (res && res.result) {
    let data = res.data
    return {
      data: data,
      result: res.result
    }
  }
}

export default {
  getUserTest,
  getTestDesc,
  getAnswer,
  saveAnswers,
  getEvaluationResult,
}
